<?php
/**
* 分页类
*/

class Pages{


	private $url;
	private $rows;
	private $show_rows;
	private $this_page;
	private $clause;
	public $start_row;
	private  $page_start;
	private $page_num;
/**
*
* @param $url 分页页面url
* @param $rows 总记录数
* @param $show_rows 每页显示记录数
* @param $this_page 当前页码
*/



public function __construct($rows,$show_rows,$this_page,$clause){

$this->rows = $rows;
$this->show_rows = $show_rows;
$this->this_page = $this_page;
$this->clause = $clause;
$this->count_page();
}

private function count_page(){

//计算总页数
$this->page_num = intval(ceil($this->rows/$this->show_rows));
//当前页码大于总页数
$this->this_page = $this->this_page > $this->page_num ? $this->page_num : $this->this_page;
//检查页码是否为正整数
$this->this_page = intval($this->this_page) == 0 ? 1 : ceil($this->this_page);
//当前页码小于0或为空或为非整数
$this->this_page = ($this->this_page < 0 || $this->this_page == '') ? 1 : $this->this_page;
//计算当前页开始记录
$this->start_row = ($this->this_page == 1 ? 0 : $this->this_page - 1) * $this->show_rows;

$this->page_start = $this->page_num < 11  ? $this->page_num : 10;

$this->page_start = $this->this_page > $this->page_start ? $this->page_start + 10 : $this->page_start;
$this->page_start = $this->this_page < $this->page_start ? $this->this_page - 1: $this->page_start;

}





//获取总页数
public function get_page_num(){
  return $this->page_num;
}


public function show_default($url){
 $this->url = empty($this->clause) ? $url.'' : $url."&".$this->clause;
 $show_page = '';
	if($this->rows != 0){
       $show_page .=  '<a href="'.$this->url.'&page=1">首 页</a>';
	   $show_page .= $this->this_page == 1 ? '<span class="disabled">上一页</span>' : '<a href="'.$this->url.'&page='.($this->this_page - 1).'">上一页</a>';
	   $show_page .= $this->this_page >= $this->page_num ? '<span class="disabled">下一页</span>' : '<a href="'.$this->url.'&page='.($this->this_page + 1).'">下一页</a>';
	   $show_page .=  '<a href="'.$this->url.'&page='.$this->page_num.'">末 页</a>';

	   $show_page .=  '<span class="disabled">共'.$this->rows.'条记录</span><span class="disabled">共'.$this->page_num.'页</span><span class="disabled">当前第'.$this->this_page.'页</span>';

	   return $show_page;

	}else{
	   return "";
	}
}
public function show_integer($url){
	if($this->rows != 0){
      if ($this->this_page > 18) $i = 18;
      else $i = 1;
      $end = $this->page_num < 20 ? $this->page_num : 20;
      if ($this->page_num > 20) $end = $this->page_num;   
	  for($i ; $i <= $end; $i++){
	  	  if (empty($show_page)) {
	  	  	$show_page = $this->this_page == $i ? "<a href='#' class='current'>".$i."</a>" : '<a href="'.$url.$i.'">'.$i.'</a>';
	  	  }else {
	  	   $show_page .=  $this->this_page == $i ? "<a href='#' class='current'>".$i."</a>" : '<a href="'.$url.$i.'">'.$i.'</a>';	
	  	  }		 
	  }
      return $show_page;
	}else{
	  return "找不到数据";
	}

return $show_page;
}


public function get_start_row(){
return $this->start_row;

}
}
?>
